/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package VcardTest;

import java.io.*;
import java.util.ArrayList;
import DataBean.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author Oran
 */
public class CSVIO {

    public boolean ReadCSV(ArrayList<Info> List, int sign) {
        String defaultFile = "./default.csv";
        BufferedReader br;
        try {
            if (sign == 1) {
                String File = GetPath();
                br = new BufferedReader(new InputStreamReader(new FileInputStream(File), "GBK"));
            } else {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(defaultFile), "GBK"));
            }

            String[] line = new String[27];
            String getline = null;
            br.readLine();
            while ((getline = br.readLine()) != null) {
                do {
                    getline = getline.concat(",无");
                    line = getline.split(",");
                } while (line.length < 27);

                //Info per = new Info();
                Basic bec = new Basic();
                Family fam = new Family();
                Network net = new Network();
                Work work = new Work();

                String name[] = new String[2];
                name[0] = line[0];
                name[1] = "";
                bec.setName(name);
                bec.setMobile(line[1].equals("") ? "无" : line[1]);
                bec.setMailbox(line[2].equals("") ? "无" : line[2]);
                bec.setBirth(line[3].equals("") ? "无" : line[3]);
                bec.setGroup(line[4].equals("") ? "无" : line[4]);
                bec.setNote(line[5].equals("") ? "无" : line[5]);

                work.setNational(line[6].equals("") ? "无" : line[6]);
                work.setProvince(line[7].equals("") ? "无" : line[7]);
                work.setCity(line[8].equals("") ? "无" : line[8]);
                work.setAddress(line[9].equals("") ? "无" : line[9]);
                work.setUnit(line[10].equals("") ? "无" : line[10]);
                work.setDepartment(line[11].equals("") ? "无" : line[11]);
                work.setPosition(line[12].equals("") ? "无" : line[12]);
                work.setCode(line[13].equals("") ? "无" : line[13]);
                work.setTelephone(line[14].equals("") ? "无" : line[14]);
                work.setFax(line[15].equals("") ? "无" : line[15]);

                net.setHomepage(line[16].equals("") ? "无" : line[16]);
                net.setCommunication(line[17].equals("") ? "无" : line[17]);
                net.setCommunication(line[18].equals("") ? "无" : line[18]);
                net.setMailbox(line[19].equals("") ? "无" : line[19]);

                fam.setFnational(line[20].equals("") ? "无" : line[20]);
                fam.setFprovince(line[21].equals("") ? "无" : line[21]);
                fam.setFcity(line[22].equals("") ? "无" : line[22]);
                fam.setFaddress(line[23].equals("") ? "无" : line[23]);
                fam.setFcode(line[24].equals("") ? "无" : line[24]);
                fam.setFtelephone(line[25].equals("") ? "无" : line[25]);
                bec.setPhotographs(line[26].equals("") ? "无" : line[26]);

                String rev = null;
                Info per = new Info(bec, fam, net, work, rev);
                int l = List.size();
                List.add(per);//在这加上插入函数！！！！
            }
            //
            br.close();
            return false;
            //}
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    public static boolean WriteCSV(ArrayList<Info> List, int sign) throws IOException {
        BufferedWriter bw = null;
        boolean ErrorSign = false;
        String defaultFile = "./default.csv";
        try {
            int t = 0;
            Info per = List.get(t);
            if (sign == 1) {
                String File = GetSavePath();
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(File), "GBK"));
            } else {
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(defaultFile), "GBK"));
            }
            bw.write("姓名" + "," + "电话" + "," + "邮箱" + "," + "生日" + "," + "所属组" + "," + "备注" + ","
                    + "工作国家" + "," + "工作省份" + "," + "工作城市" + "," + "工作地址" + "," + "工作单位" + "," + "工作部门" + "," + "职位" + "," + "邮政编码" + "," + "工作电话" + "," + "传真" + ","
                    + "个人主页" + "," + "QQ号码" + "," + "MSN号码" + "," + "备用邮箱" + ","
                    + "家所在国家" + "," + "家所在省份" + "," + "家所在城市" + "," + "家庭地址" + "," + "家庭邮编" + "," + "家庭电话" + "照片地址");
            //for (int i = 0; i < al.size(); i++) {
            bw.newLine();
            bw.write(per.getBec().getName() + "," + per.getBec().getMobile() + "," + per.getBec().getMailbox() + "," + per.getBec().getBirth() + "," + per.getBec().getGroup() + "," + per.getBec().getNote() + ","
                    + per.getWork().getNational() + "," + per.getWork().getProvince() + "," + per.getWork().getCity() + "," + per.getWork().getAddress() + "," + per.getWork().getUnit() + "," + per.getWork().getDepartment() + "," + per.getWork().getPosition() + "," + per.getWork().getCode() + "," + per.getWork().getTelephone() + "," + per.getWork().getFax() + ","
                    + per.getNet().getHomepage() + "," + per.getNet().getCommunication() + "," + per.getNet().getMailbox() + ","
                    + per.getFam().getFnational() + "," + per.getFam().getFnational() + "," + per.getFam().getFcity() + "," + per.getFam().getFaddress() + "," + per.getFam().getFcode() + "," + per.getFam().getFtelephone() + "," + per.getBec().getPhotographs());
            //}
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String GetPath() {
        String path = null;
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("txt", "dat");
        int returnVal = chooser.showOpenDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = chooser.getSelectedFile().getAbsolutePath();
        }
        return path;
    }

    public static String GetSavePath() {
        String path = null;
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("txt", "dat");
        int returnVal = chooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = chooser.getSelectedFile().getAbsolutePath();
        }
        return path;
    }
}
